package com.qding.park.manager.web;

import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Filter;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import org.springframework.web.filter.CharacterEncodingFilter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.qding.park.manager.web.conf.SSOConfig;
import com.qding.park.manager.web.filter.SimpleCORSFilter;
import com.qding.smart.sso.client.filter.SSOFilter;

@Configuration
@ComponentScan(basePackages = { "com.baidu", "com.qding", "com.qding.park.manager.web.conf" })
@ImportResource({ "classpath:disconf.xml","classpath:spring-interceptor.xml"  }) // 引入disconf,spring-interceptor
@PropertySource({ "classpath:application.properties" })
@ServletComponentScan
@Order(HIGHEST_PRECEDENCE)
public class ParkWebApplication extends SpringBootServletInitializer {
    
	@Autowired
	private  SSOConfig   ssoConfig;
	
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
		supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		fastConverter.setSupportedMediaTypes(supportedMediaTypes);
		
		ValueFilter filterObj = new ValueFilter() {
		    @Override
		    public Object process(Object obj, String s, Object v) {
		    
		    JSONObject jsonObject=JSONObject.parseObject(obj.toString());
		   Object value= jsonObject.get(s);
		    if(v==null&&(value instanceof String))
		        return "";
		    return v;
		    }
		};
		
		HttpMessageConverter<?> converter = fastConverter;
		HttpMessageConverters httpConverter = new HttpMessageConverters(converter);
		return httpConverter;

	}

	
	@Bean
	public FilterRegistrationBean filterSimpleCORSFilterBean() {
		FilterRegistrationBean registrationBean = new FilterRegistrationBean();
		SimpleCORSFilter corsFilter = new SimpleCORSFilter();
		registrationBean.setFilter(corsFilter);
		List<String> urlPatterns = new ArrayList<String>();
		urlPatterns.add("/*");
		registrationBean.setUrlPatterns(urlPatterns);
		registrationBean.setOrder(Integer.MAX_VALUE-10);
		return registrationBean;
	}

	// 用于处理编码问题
	@Order(Ordered.HIGHEST_PRECEDENCE)
	@Bean
	public Filter characterEncodingFilter() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setEncoding("UTF-8");
		characterEncodingFilter.setForceEncoding(true);
		return characterEncodingFilter;
	}
	

	public static void main(String[] args) throws Exception {
		SpringApplication.run(ParkWebApplication.class, args);
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

		return application.sources(ParkWebApplication.class);
	}

}
